package com.ang.normal.routing;

import com.ang.normal.RabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ssang
 * @date 2022-04-09 12:57
 */
public class RoutConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMqUtils.getRabbitMqConnection();

        Channel channel = connection.createChannel();

        //通道申明交换机和交换机类型
        channel.exchangeDeclare("orders","direct");

        //如果没有queue，可以创建临时队列
        String queueName = channel.queueDeclare().getQueue();

        //通过路由键，绑定交换机和队列
        String routKey = "orderKey";
        channel.queueBind(queueName,"orders",routKey);

        //获取消费的消息
        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者: "+ new String(body));
            }
        });
    }
}
